package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.SelectVo;
import com.zb.entity.ZjProduct;
import com.zb.entity.ZjProductBoom;
import com.zb.entity.ZjProductSpec;
import com.zb.mapper.ZjProductBoomMapper;
import com.zb.service.ZjProductBoomChildService;
import com.zb.service.ZjProductBoomService;
import com.zb.service.ZjProductService;
import com.zb.service.ZjProductSpecService;
import com.zb.vo.ProductBoomCountVo;
import com.zb.vo.ProductBoomListVo;
import com.zb.vo.ProductBoomVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 产品boom表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-22
 */
@Service
public class ZjProductBoomServiceImpl extends ServiceImpl<ZjProductBoomMapper, ZjProductBoom> implements ZjProductBoomService {
    @Resource
    private ZjProductBoomMapper productBoomMapper;
    @Resource
    private ZjProductBoomChildService childService;
    @Resource
    private ZjProductService productService;
    @Resource
    private ZjProductSpecService productSpecService;

    // 新增、修改Boom关联的商品
    @Override
    @Transactional
    public void save(Long custAccId, ProductBoomVo boomVo) {
        Assert.isTrue(null == productBoomMapper.existByGoodsSpecId(boomVo.getId(), boomVo.getProductId(), boomVo.getProductSpecId()), "商品规格的Boom已重复关联！");
        ZjProductBoom boom = new ZjProductBoom();
        BeanUtils.copyProperties(boomVo, boom);
        if (null != boom.getId()) {
            this.updateById(boom);
        } else {
            boom.setId(IdWorker.getId());
            boom.setCustAccId(custAccId);
            this.save(boom);
        }
        childService.save(custAccId, boom.getId(), boomVo.getBoomChildVos(), boomVo.getDelBoomChildIds());
    }

    // 根据产品Boom ID获取产品Boom详情
    @Override
    public ProductBoomVo getInfoById(Long id) {
        ZjProductBoom boom = this.getById(id);
        if (null == boom) {
            return null;
        }
        ProductBoomVo vo = new ProductBoomVo();
        BeanUtils.copyProperties(boom, vo);
        return vo;
    }

    // 根据产品ID获取产品Boom清单
    @Override
    public List<ProductBoomListVo> getByProductId(Long productId) {
        List<ZjProductBoom> boomList = productBoomMapper.getByProductId(productId);
        if (null == boomList || boomList.isEmpty()) {
            return new ArrayList<>();
        }
        List<ProductBoomCountVo> countVos = childService.getCountByProductBoomIds(boomList.stream().map(ZjProductBoom::getId).toList());

        QueryWrapper<ZjProduct> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.select("model", "name", "type");
        productQueryWrapper.eq("id", productId);
        ZjProduct product = productService.getOne(productQueryWrapper);

        QueryWrapper<ZjProductSpec> specQueryWrapper = new QueryWrapper<>();
        specQueryWrapper.select("id", "name", "code");
        specQueryWrapper.in("id", boomList.stream().map(ZjProductBoom::getProductSpecId).toList());
        List<ZjProductSpec> specList = productSpecService.list(specQueryWrapper);

        return boomList.stream().map(boom -> {
            ProductBoomListVo vo = new ProductBoomListVo();
            BeanUtils.copyProperties(boom, vo);
            vo.setModel(product.getModel());
            vo.setType(product.getType());
            vo.setName(product.getName());
            ZjProductSpec productSpec = specList.stream().filter(spec -> Objects.equals(boom.getProductSpecId(), spec.getId())).findFirst().orElse(null);
            if (null != productSpec) {
                vo.setSpecName(productSpec.getName());
                vo.setSpecCode(productSpec.getCode());
            }
            vo.setQty(countVos.stream().filter(countVo -> Objects.equals(vo.getId(), countVo.getId())).map(ProductBoomCountVo::getQty).findFirst().orElse(0));
            return vo;
        }).toList();
    }

    // 根据产品ID & 产品规格ID 获取boom
    @Override
    public ZjProductBoom getByProductIdAndProductSpecId(Long productId, Long productSpecId) {
        QueryWrapper<ZjProductBoom> wrapper = new QueryWrapper<>();
        wrapper.select("id", "product_id", "product_spec_id");
        wrapper.eq("product_id", productId).eq("product_spec_id", productSpecId);
        return this.getOne(wrapper);
    }

    // 根据工单ID获取商品关联的boom清单的配件ID
    @Override
    public List<Long> getSpareIdsPageSelect(Long workId, SelectVo selectVo) {
        return productBoomMapper.getSpareIdsPageSelect(workId, selectVo);
    }

    // 根据工单ID获取商品关联的boom清单的配件ID的总记录数
    @Override
    public long getSpareIdsPageSelectCount(Long workId, SelectVo selectVo) {
        return productBoomMapper.getSpareIdsPageSelectCount(workId, selectVo);
    }
}
